<!DOCTYPE html>
<!--

// Copyright 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

-->
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="../../../third_party/closure-library/closure/goog/base.js"></script>
<script data-scan>
goog.require('goog.testing.jsunit');
</script>
<script src="../../../src/traceur.js" data-scan></script>
</head>
<body>
<script data-scan>

var errorReporter = {
  reportError: function(position, message) {
    fail(message + ', ' + position);
  }
};

function testScanner() {
  var program = 'null';
  var sourceFile = new traceur.syntax.SourceFile('Name', program);
  var scanner = new traceur.syntax.Scanner(errorReporter, sourceFile);

  var token = scanner.peekToken();
  assertEquals(traceur.syntax.TokenType.NULL, token.type);
}

function testEmpty() {
  var program = '';
  var sourceFile = new traceur.syntax.SourceFile('Name', program);
  var scanner = new traceur.syntax.Scanner(errorReporter, sourceFile);

  var token = scanner.peekToken();
  assertEquals(traceur.syntax.TokenType.END_OF_FILE, token.type);
}

function scanScript(scriptElement) {
  var source;
  var name;
  if (scriptElement.src) {
    name = scriptElement.src;
    var xhr = new XMLHttpRequest();
    // Synchronous load but these are already loaded when we get here so it
    // should be fast.
    xhr.open('GET', scriptElement.src, false);
    xhr.send();
    source = xhr.responseText;
  } else {
    name = 'inline-script';
    source = scriptElement.textContent;
  }

  var sourceFile = new traceur.syntax.SourceFile(name, source);
  var scanner = new traceur.syntax.Scanner(errorReporter, sourceFile);

  console.log('Scanning ', name);

  return scanToString(scanner);
}

function scanToString(scanner) {
  var next;
  var s = '';
  while ((next = scanner.nextToken()) &&
         next.type !== traceur.syntax.TokenType.END_OF_FILE) {
    s += next;
  }
  return s;
}

function testScanAllScripts() {
  // We cannot scan all scripts due to regular expression literals which
  // requires a parser.
  var scripts = document.querySelectorAll('script');
  for (var i = 0, script; script = scripts[i]; i++) {
    if (script.hasAttribute('data-scan'))
      scanScript(script);
  }
}

function testScanner2() {
  // Note the florin unicode character in the function name.
  var source = 'function ƒ(x, y) { return x + 42; }\n' +
      'var world = true - "hello";\n';
  var sourceFile = new traceur.syntax.SourceFile(name, source);
  var scanner = new traceur.syntax.Scanner(errorReporter, sourceFile);

  assertEquals('functionƒ(x,y){returnx+42;}varworld=true-"hello";',
               scanToString(scanner));
}

function testScanner3() {
  // Regression test...
  var source = '[format].concat(args);\n';
  var sourceFile = new traceur.syntax.SourceFile(name, source);
  var scanner = new traceur.syntax.Scanner(errorReporter, sourceFile);

  function next() {
    return String(scanner.nextToken());
  }

  assertEquals('[', next());
  assertEquals('format', next());
  assertEquals(']', next());
  assertEquals('.', next());
  assertEquals('concat', next());
  assertEquals('(', next());
  assertEquals('args', next());
  assertEquals(')', next());
  assertEquals(';', next());
  assertEquals('End of File', next());
}

function testScannerAwait() {
  var source = 'await x;\n';
  var sourceFile = new traceur.syntax.SourceFile(name, source);
  var scanner = new traceur.syntax.Scanner(errorReporter, sourceFile);

  function next() {
    return scanner.nextToken().type;
  }

  var TokenType = traceur.syntax.TokenType;

  assertEquals(TokenType.AWAIT, next());
  assertEquals(TokenType.IDENTIFIER, next());
  assertEquals(TokenType.SEMI_COLON, next());
  assertEquals(TokenType.END_OF_FILE, next());
}

</script>
</body>
</html>
